package net.lcheng.shop.Controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.lcheng.commons.utils.UploadFileHelper;
import net.lcheng.commons.utils.UploadFileResult;
import net.lcheng.utils.UploadResultModel;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by qiliping on 16/7/20.
 */
@RestController
public class UploadImgController {

    private static Logger log = Logger.getLogger(UploadImgController.class);

    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/file_upload", method = RequestMethod.POST)
    public UploadResultModel fileUpload(HttpServletRequest request, HttpServletResponse response, @RequestParam String img)
            throws Exception {
        String key = "1b04c515b73755da703881d2b858261b";
        String userName = "zhaorenxue@123001";

        Properties properties = PropertiesLoaderUtils.loadAllProperties("config.properties");
        String uploadUrl = properties.getProperty("uploadUrl");
        return UploadFileHelper.uploadBase64Image(uploadUrl,key,userName,img.replace("data:image/jpeg;base64,",""));


      /*  UploadResultModel resultModel = new UploadResultModel();
        resultModel.setCode(-1);

        log.info("开始上传");
        if (StringUtils.isEmpty(img)) {

            resultModel.setMsg("IMG IS NULL");
            //图像数据为空
            return resultModel;
        }

        ServletContext application = request.getSession().getServletContext();
        String savePath = application.getRealPath("/") + "attached/";

        // 文件保存目录URL
        String saveUrl = request.getContextPath() + "/attached/";

        String fileExt = "jpg";
        Properties properties = PropertiesLoaderUtils.loadAllProperties("config.properties");
        String imgDomain = properties.getProperty("imgDomain");


        log.info("创建文件夹");
        // 创建文件夹
        File saveDirFile = new File(savePath);
        if (!saveDirFile.exists()) {
            saveDirFile.mkdirs();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String ymd = sdf.format(new Date());
        savePath += ymd + "/";
        saveUrl += ymd + "/";
        File dirFile = new File(savePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }


        log.info("开始读取base64");

        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000);


        try {
            boolean returnResult = UploadImgUtils.base64ToImageOutput(img.replace("data:image/jpeg;base64,",""), savePath, newFileName,fileExt);
            if(returnResult){
                resultModel.setCode(0);
                resultModel.setUrl(imgDomain+saveUrl+newFileName+"."+fileExt);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return resultModel;*/
    }
}
